﻿using System;
using System.Threading.Tasks;
using com.IronOne.BoardPACWinAppBO.Settings;
using com.IronOne.BoardPACWinAppDAO.DbOperations;
using com.IronOne.BoardPACWinAppDAO.Init;
using com.IronOne.BoardPACWinAppService.Xml;
using com.IronOne.BoardPACWinAppUtil.Util;

namespace com.IronOne.BoardPACWinAppService.Init
{
    public class InitService : IInitService
    {
        private readonly IDbOperations _iDbOperations;
        private readonly IInitDAO _initDAO;

        public InitService()
        {
            _iDbOperations = new DbOperations();
            _initDAO = new InitDAO();
        }

        public bool CreateGeneralDatabase()
        {
            return _iDbOperations.CreateGeneralDatabase();
        }

        public async Task<AppSettingModel> ReadAppSettingsFromDb()
        {
            try
            {
                return await _initDAO.SelectAllAppSettings();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public async Task<bool> WriteAppSettingsToDb(AppSettingModel asm)
        {
            try
            {
                return await _initDAO.InsertAppSettingsAsync(asm);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public async Task<bool> WriteDateFormatsNSyncFreqsToDb(AppSettingModel asm)
        {
            try
            {
                return await _initDAO.InsertDateFormatsNSyncFreqsAsync(asm);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public async Task<AppSettingModel> InitDevice(AppSettingModel apSettingModel)
        {
            var asm = apSettingModel;
            try
            {
                var responseModel =
                    await RequestManager.CreateRequest(null, RequestURL.Init, ApplicationConstants.RequestType.GET);

                if (responseModel.IsError)
                {
                    asm.IsError = responseModel.IsError;
                    asm.ErrorMessage = responseModel.ErrorMessage;
                    return asm;
                }

                asm = XmlToModelMapper.AppSettingModelMapper(asm, responseModel);
            }
            catch (Exception)
            {
                asm.IsError = true;
                asm.ErrorMessage = "Service general error";
            }
            return asm;
        }

        public async Task<bool> ServerUpCheck()
        {
            try
            {
                var responseModel =
                    await RequestManager.CreateRequest(null, RequestURL.ServerUp, ApplicationConstants.RequestType.GET);

                return !responseModel.IsError;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public async Task<string> GetDateFormat()
        {
            try
            {
                return await _initDAO.GetDateFormat();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}